<?php

class Client_model extends MY_Model {

    public function __construct() {
	parent::__construct();
	$this->table = 'alsa_client';

	$this->as = 'c';

	$this->relations = array(
	    array(
		'table' => 'alsa_city',
		'as' => 'ci',
		'foreign' => 'id',
		'local' => 'city',
		'type' => 'left'
	    ),
	    array(
		'table' => 'alsa_service',
		'as' => 's',
		'foreign' => 'id',
		'local' => 'market',
		'type' => 'left'
	    )
	);

	$this->columns = array(
	    '*',
	    array('ci.name_zh', 'city_name'),
	    array('s.menu_zh', 'market_menu')
	);
    }

    public function cities_by_market($market, $lang) {
	$sql = "select
		    ci.id as id,
		    ci.type as type,
		    ci.name_{$lang} as name,
		    ci.position_x as position_x,
		    ci.position_y as position_y,
		    c.market as market
		from alsa_client c
		left join alsa_city ci
		    on c.city=ci.id
		where c.market={$market}
		group by c.city;";

	$query = $this->db->query($sql);
	return $query->result();
    }

}
